// 在预加载脚本中
const { ipcRenderer, contextBridge } = require('electron');

contextBridge.exposeInMainWorld('useElectronApi', {
  alertSysMsg: (arg) => {
    ipcRenderer.send('alertSysMsg', { title: arg.title, body: arg.body });
  },
  checkUpdate: () => {
    ipcRenderer.send('checkUpdate');
  },
  confirmDownloadUpdate: () => {
    ipcRenderer.send('confirm-downloadUpdate');
  },
  confirmUpdate: () => {
    ipcRenderer.send('confirm-update');
  },
});

contextBridge.exposeInMainWorld(
  'registerEvent',
  function (eventName, callback) {
    ipcRenderer.removeAllListeners(eventName);
    ipcRenderer.on(eventName, function (e, args) {
      callback(args);
    });
  },
);

contextBridge.exposeInMainWorld('removeEvent', function (eventName) {
  ipcRenderer.removeAllListeners(eventName);
});
